package com.nys.cqhttp.message.factory.impl;

import com.nys.cqhttp.mapper.*;
import com.nys.cqhttp.message.factory.IMessageHandler;
import com.nys.cqhttp.model.*;
import com.nys.cqhttp.param.Context;
import com.nys.cqhttp.param.MessageParam;
import com.nys.cqhttp.util.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.apache.commons.collections.CollectionUtils;

import javax.annotation.PostConstruct;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * DefaultMessageHandler
 *
 * @author nysheng
 * @version 1.0.0
 * @description 消息默认处理
 * @date 2021/12/28 15:44
 */
@Component
@Slf4j
public class ShopMessageHandler implements IMessageHandler {
  @Autowired private UserBackpackMapper userBackpackMapper;
  @Autowired private SysUserMapper sysUserMapper;
  @Autowired private ShopConfigMapper shopConfigMapper;
  @Autowired private ShopLogMapper shopLogMapper;
  @Autowired private MessageLogMapper messageLogMapper;

  private static ShopMessageHandler shopMessageHandler;

  private static String at = "[CQ:at,qq=";
  private static String at1 = "]";

  @PostConstruct
  public void init() throws Exception {
    shopMessageHandler = this;
    sysUserMapper = this.sysUserMapper;
    userBackpackMapper = this.userBackpackMapper;
    shopConfigMapper = this.shopConfigMapper;
    shopLogMapper = this.shopLogMapper;
    messageLogMapper = this.messageLogMapper;
  }

  @Override
  public List<String> listHandler(Massage messagePojo, Context context) {
    return Arrays.asList(handler(messagePojo, context));
  }

  @Override
  public String handler(Massage messagePojo, Context context) {
    try {
      Random random = new Random();
      MessageParam messageParam = context.getMessageParam();
      String message = messageParam.getMessage();
      String senderId = messageParam.getSenderId();
      String selfId = messageParam.getSelfId();
      if (message.equals("商店")) {
        String res = "商品如下:\n商品名称\t单价\t剩余\t每人限购";
        ShopConfigExample shopConfigExample = new ShopConfigExample();
        ShopConfigExample.Criteria criteria = shopConfigExample.createCriteria();
        criteria.andJqrQqEqualTo(selfId);
        List<ShopConfig> shopConfigs =
            shopMessageHandler.shopConfigMapper.selectByExample(shopConfigExample);
        if (CollectionUtils.isEmpty(shopConfigs)) {
          return at + messageParam.getSenderId() + at1 + "当前没有出售商品!";
        } else {
          for (ShopConfig shopConfig : shopConfigs) {
            res +=
                "\n"
                    + shopConfig.getName()
                    + "✨"
                    + shopConfig.getPrice()
                    + "✨"
                    + shopConfig.getNum()
                    + "✨"
                    + shopConfig.getPersonNum();
          }
          res += "\n购买请发送:购买-x*y x商品名 y数量";
        }
        return at + messageParam.getSenderId() + at1 + res;
      } else if (message.contains("购买")) {
        String[] split = message.split("-");
        String[] split1 = split[1].split("\\*");
        Integer num = Integer.valueOf(split1[1]);
        ShopConfigExample shopConfigExample = new ShopConfigExample();
        ShopConfigExample.Criteria criteria = shopConfigExample.createCriteria();
        criteria.andJqrQqEqualTo(selfId);
        criteria.andNameEqualTo(split1[0]);
        List<ShopConfig> shopConfigs =
            shopMessageHandler.shopConfigMapper.selectByExample(shopConfigExample);
        if (CollectionUtils.isEmpty(shopConfigs)) {
          return at + messageParam.getSenderId() + at1 + "商店没有" + split1[0] + "商品";
        }
        ShopConfig shopConfig = shopConfigs.get(0);
        log.info((shopConfig.getNum() < num)+""+shopConfig.getNum()+""+ num);
        if (shopConfig.getNum() < num) {
          return at + messageParam.getSenderId() + at1 + "商店该商品货源不足!";
        }
        SysUserExample sysUserExample = new SysUserExample();
        SysUserExample.Criteria criteria1 = sysUserExample.createCriteria();
        criteria1.andJqrQqEqualTo(messageParam.getSelfId());
        criteria1.andUserQqEqualTo(messageParam.getSenderId());
        List<SysUser> sysUsers = shopMessageHandler.sysUserMapper.selectByExample(sysUserExample);
        if (CollectionUtils.isEmpty(sysUsers)) {
          return at + messageParam.getSenderId() + at1 + "当前QQ未创建账号,请发送'签到'创建账号!";
        }
        SysUser sysUser = sysUsers.get(0);
        if (sysUser.getGold() < num * shopConfig.getPrice()) {
          return at + messageParam.getSenderId() + at1 + "你的金币不足!";
        }
        ShopLogExample shopLogExample = new ShopLogExample();
        ShopLogExample.Criteria criteria2 = shopLogExample.createCriteria();
        criteria2.andJqrQqEqualTo(selfId);
        criteria2.andUserQqEqualTo(senderId);
        criteria2.andNameEqualTo(shopConfig.getName());
        criteria2.andSendDateGreaterThan(DateUtils.getNowDayStart());
        List<ShopLog> shopLogs = shopMessageHandler.shopLogMapper.selectByExample(shopLogExample);
        int i = 0;
        if (!CollectionUtils.isEmpty(shopLogs)) {
          i = shopLogs.get(0).getNum();
        }
        if (i + num > shopConfig.getPersonNum()) {
          return at + messageParam.getSenderId() + at1 + "你今日购买该商品次数不足!";
        }
        MessageLogExample messageLogExample = new MessageLogExample();
        MessageLogExample.Criteria criteria3 = messageLogExample.createCriteria();
        criteria3.andJqrQqEqualTo(selfId);
        criteria3.andUserQqEqualTo(senderId);
        criteria3.andMessageLike("游戏2-");
        criteria3.andSendDateGreaterThan(DateUtils.getNowDayStart());
        List<MessageLog> messageLogs =
            shopMessageHandler.messageLogMapper.selectByExample(messageLogExample);
        if (CollectionUtils.isNotEmpty(messageLogs)) {
          MessageLogExample messageLogExample2 = new MessageLogExample();
          MessageLogExample.Criteria criteria4 = messageLogExample2.createCriteria();
          criteria4.andJqrQqEqualTo(selfId);
          criteria4.andUserQqEqualTo(senderId);
          criteria4.andMessageLike("结束钓鱼-");
          criteria4.andSendDateGreaterThan(DateUtils.getNowDayStart());
          List<MessageLog> messageLogs1 =
              shopMessageHandler.messageLogMapper.selectByExample(messageLogExample2);
          if (CollectionUtils.isEmpty(messageLogs1)) {
            return at + messageParam.getSenderId() + at1 + "正在钓鱼,不能购买商品";
          }
        }
        ShopLog shopLog = new ShopLog();
        if (CollectionUtils.isEmpty(shopLogs)) {
          shopLog.setJqrQq(selfId);
          shopLog.setNum(num);
          shopLog.setSendDate(new Date());
          shopLog.setUserQq(senderId);
          shopLog.setName(split1[0]);
          shopMessageHandler.shopLogMapper.insertSelective(shopLog);
        } else {
          shopLog = shopLogs.get(0);
          shopLog.setNum(shopLog.getNum() + num);
          shopMessageHandler.shopLogMapper.updateByPrimaryKeySelective(shopLog);
        }
        sysUser.setGold(sysUser.getGold() - Integer.valueOf(split1[1]) * shopConfig.getPrice());

        shopMessageHandler.sysUserMapper.updateByPrimaryKeySelective(sysUser);
        UserBackpackExample userBackpackExample = new UserBackpackExample();
        UserBackpackExample.Criteria criteria6 = userBackpackExample.createCriteria();
        criteria6.andJqrQqEqualTo(messageParam.getSelfId());
        criteria6.andUserQqEqualTo(messageParam.getSenderId());
        List<UserBackpack> userBackpacks =
            shopMessageHandler.userBackpackMapper.selectByExample(userBackpackExample);
        UserBackpack userBackpack = new UserBackpack();
        if (CollectionUtils.isEmpty(userBackpacks)) {
          userBackpack.setJqrQq(messageParam.getSelfId());
          userBackpack.setUserQq(messageParam.getSenderId());
          if ("精品鱼饵".equals(split1[0])) {
            userBackpack.setBoutiqueBait(num);
          }
          if ("优质鱼饵".equals(split1[0])) {
            userBackpack.setPremiumBait(num);
          }
          if ("普通鱼饵".equals(split1[0])) {
            userBackpack.setPlainBait(num);
          }
          shopMessageHandler.userBackpackMapper.insertSelective(userBackpack);
        } else {
          userBackpack = userBackpacks.get(0);
          if ("精品鱼饵".equals(split1[0])) {
            userBackpack.setBoutiqueBait(userBackpack.getBoutiqueBait() + num);
          }
          if ("优质鱼饵".equals(split1[0])) {
            userBackpack.setPremiumBait(userBackpack.getPremiumBait() + num);
          }
          if ("普通鱼饵".equals(split1[0])) {
            userBackpack.setPlainBait(userBackpack.getPlainBait() + num);
          }
          shopConfig.setNum(shopConfig.getNum() - num);
          shopMessageHandler.shopConfigMapper.updateByPrimaryKeySelective(shopConfig);
          shopMessageHandler.userBackpackMapper.updateByPrimaryKeySelective(userBackpack);
        }
        return "购买成功!";
      }else if (message.contains("补货")){
        String[] split = message.split("-");
        String[] split1 = split[1].split("\\*");

        ShopConfigExample shopConfigExample = new ShopConfigExample();
        ShopConfigExample.Criteria criteria = shopConfigExample.createCriteria();
        criteria.andJqrQqEqualTo(selfId);
        criteria.andNameEqualTo(split1[0]);
        List<ShopConfig> shopConfigs =
                shopMessageHandler.shopConfigMapper.selectByExample(shopConfigExample);
        if (CollectionUtils.isEmpty(shopConfigs)){
          return "没有该商品货源!";
        }
        ShopConfig shopConfig = shopConfigs.get(0);
        shopConfig.setNum(shopConfig.getNum()+Integer.valueOf(split1[1]));
        shopMessageHandler.shopConfigMapper.updateByPrimaryKeySelective(shopConfig);
        return "补货成功!";
      }
      return "12";
    } catch (Exception e) {
      e.printStackTrace();
      return e.getMessage();
    }
  }
}
